package com.easyboot.web.system.controller;

import cn.hutool.core.util.StrUtil;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.framework.domain.KeyValueInt64Dto;
import com.easyboot.framework.domain.ResultAdaptDto;
import com.easyboot.framework.shiro.UserOwnInfo;
import com.easyboot.framework.core.ActionResult;
import com.easyboot.framework.core.BaseController;
import com.easyboot.framework.core.RequestHelper;
import com.easyboot.web.system.domain.SysDictionary;
import com.easyboot.web.system.service.SysDictionaryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/24 16:05
 **/
@RequestMapping("/admin/sysdic")
@Controller
public class SysDicController extends BaseController {
    @Autowired
    private RequestHelper requestHelper;
    @Autowired
    private SysDictionaryService dictionaryService;

    @RequiresPermissions("system:dic:view")
    @GetMapping("/index")
    public ActionResult index(){

        return view();
    }

    /**
    *
     * @author yushuo
     * @description 获取字典列表
     * @date 16:11 2021/2/24
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions("system:dic:view")
    @PostMapping("getList")
    public ActionResult getList(@ModelAttribute DataTableDto dto){

        String code=requestHelper.getString("dicCode","");
        String name=requestHelper.getString("name","");

        dto= dictionaryService.getList(dto,code,name);
        return json(dto);
    }

    /**
    *
     * @author yushuo
     * @description 新增和修改页面
     * @date 16:54 2021/2/24
     * @param [id, model]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value = {"system:dic:add","system:dic:edit"},logical = Logical.OR)
    @GetMapping("/form")
    public ActionResult form(String id, Model model){

        model.addAttribute("id",id);
        return view();
    }

    /**
     *
     * @author yushuo
     * @description 获取单个实体
     * @date 16:54 2021/2/24
     * @param [id, model]
     * @return com.easyboot.framework.web.ActionResult
     **/
    @RequiresPermissions(value = {"system:dic:add","system:dic:edit"},logical = Logical.OR)
    @GetMapping("/getModel")
    public ActionResult getModel(Long id){
        ResultAdaptDto result=new ResultAdaptDto();
        SysDictionary dict= dictionaryService.getModel(id);
        result.put("model",dict);
        return json(result);
    }

    /**
    *
     * @author yushuo
     * @description 提交数据
     * @date 16:57 2021/2/24
     * @param [dic]
     * @return com.easyboot.framework.web.ActionResult
    **/
    @RequiresPermissions(value = {"system:dic:add","system:dic:edit"},logical = Logical.OR)
    @PostMapping("/save")
    public ActionResult save(SysDictionary dic){

        UserOwnInfo userOwnInfo=requestHelper.getUserOwnInfo();
        dictionaryService.saveDic(dic,userOwnInfo);
        return ok();
    }

    /**
     *
     * @author yushuo
     * @description 删除数据
     * @date 16:57 2021/2/24
     * @param [dic]
     * @return com.easyboot.framework.web.ActionResult
     **/
    @GetMapping("/removeAll")
    @RequiresPermissions("system:dic:delete")
    public ActionResult removeAll(String ids){
        if(StrUtil.isEmpty(ids)){
            return error("参数验证失败");
        }
        dictionaryService.removeAll(ids);
        return ok("字典项删除成功");
    }

    /**
    *
     * @author yushuo
     * @description 字典子项页面
     * @date 17:06 2021/2/24
     * @param []
     * @return com.easyboot.framework.web.ActionResult
    **/
    @GetMapping("/sonView")
    @RequiresPermissions("system:dic:son")
    public ActionResult sonView(Model model){

        Long parentId=requestHelper.getInt64("parentId",0L);
        SysDictionary parentDict= dictionaryService.getModel(parentId);
        List<KeyValueInt64Dto> dictList=dictionaryService.getDictSelect();
        model.addAttribute("parentDict",parentDict);
        model.addAttribute("dictList",dictList);
        return view();
    }


    /**
     *
     * @author yushuo
     * @description 获取字典列表
     * @date 16:11 2021/2/24
     * @param [dto]
     * @return com.easyboot.framework.web.ActionResult
     **/
    @RequiresPermissions("system:dic:son")
    @PostMapping("getSonList")
    public ActionResult getSonList(@ModelAttribute DataTableDto dto){

        Long parentId=requestHelper.getInt64("pid",0L);
        String name=requestHelper.getString("name","");

        dto= dictionaryService.getSonList(dto,parentId,name);
        return json(dto);
    }
    /**
     *
     * @author yushuo
     * @description 字典子项页面
     * @date 17:06 2021/2/24
     * @param []
     * @return com.easyboot.framework.web.ActionResult
     **/
    @GetMapping("/sonForm")
    @RequiresPermissions("system:dic:son")
    public ActionResult sonForm(Model model){
        String id=requestHelper.getString("id","0");
        Long parentId=requestHelper.getInt64("parentId",0L);
        SysDictionary parentDict= dictionaryService.getModel(parentId);
        List<KeyValueInt64Dto> dictList=dictionaryService.getDictSelect();
        model.addAttribute("id",id);
        model.addAttribute("parentDict",parentDict);
        model.addAttribute("dictList",dictList);
        return view();
    }
}
